package com.example.adapter.demo.login.v2;

import com.example.adapter.demo.login.v2.adapter.*;

/**
 * @author zhoubin
 */
public class PassportForThirdAdapter implements IPassportForThird {
    @Override
    public ResultMsg loginForQQ(String openId) {
        return processLogin(openId, LoginForQQAdapter.class);
    }

    @Override
    public ResultMsg loginForWechat(String openId) {
        return processLogin(openId, LoginForWechatAdapter.class);
    }

    @Override
    public ResultMsg loginForToken(String token) {
        return processLogin(token, LoginForTokenAdapter.class);
    }

    @Override
    public ResultMsg loginForTelphone(String phone, String code) {
        return processLogin(phone, LoginForTelphoneAdapter.class);
    }


    public ResultMsg processLogin(String id, Class<? extends ILoginAdapter> clazz) {
        try {
            ILoginAdapter loginAdapter = clazz.newInstance();
            if (loginAdapter.support(loginAdapter)) {
                return loginAdapter.login(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
